package lib.bone.access.module.dict.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dao.NepuTopicDictDAO;
import lib.bone.access.module.dict.entity.NepuTopicDict;
import lib.bone.access.module.dict.DictService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * cg
 */
@Slf4j
@Service
public class TopicDictServiceImpl implements DictService<NepuTopicDict,String> {
    @Autowired
    NepuTopicDictDAO nepuTopicDictDAO;

//    @Autowired
//    NepuTopicDAO topicDAO;

    @Override
    public String getServiceType() {
        return ResearchTypeConst.TOPIC;
    }

    public NepuTopicDict getTopicType(String type) {
        return nepuTopicDictDAO.findByTopicTypeLike("%"+type+"%");
    }



    @Override
    public PageResultDTO<NepuTopicDict> getAll(Pageable pageable, Example<NepuTopicDict> example) {

        Page<NepuTopicDict> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = nepuTopicDictDAO.findAll(pageable);
        }else {
            page = nepuTopicDictDAO.findAll(example,pageable);
        }
        List<NepuTopicDict> nepuTopicDictList = page.getContent();
        PageResultDTO<NepuTopicDict> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(nepuTopicDictList);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;    }

    @Override
    public ResponseDTO<NepuTopicDict> deleteById(String id) {
//        Boolean useExist = topicDAO.existsByTopicDictId(id);
//        if(useExist){
//            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该词典的奖励信息,拒绝执行删除!");
//        }
        Optional<NepuTopicDict> nepuTopicDictOp = getById(id);
        if(!nepuTopicDictOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"未找到ID:["+id+"]对应资源!");
        nepuTopicDictDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");    }

    @Override
    public Optional<NepuTopicDict> getById(String id) {
        return nepuTopicDictDAO.findById(id);
    }

    @Override
    public NepuTopicDict saveOne(NepuTopicDict nepuTopicDict) {
        if (Validator.isNotEmpty(nepuTopicDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuTopicDictDAO.save(nepuTopicDict);    }

    @Override
    public NepuTopicDict updateOne(NepuTopicDict nepuTopicDict) {
        if (Validator.isEmpty(nepuTopicDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuTopicDictDAO.save(nepuTopicDict);
    }
}
